package com.burgeon.backend.EnterpriseWeChat.rest.VipInfo;

import com.burgeon.backend.EnterpriseWeChat.entity.VipContact;
import com.burgeon.backend.EnterpriseWeChat.entity.VipFamily;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 其他联系方式
 */
public class getVipContactInfo extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getVipContactInfo(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public VipContact getData(String vipId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(vipId), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
//        ArrayList<VipContact> vipContacts = new ArrayList<>();
//        for (int i = 0; i <jsonArray.length() ; i++) {

        if (jsonArray.length()>0){
            VipContact vipContact= new VipContact();
            vipContact.setPhone(jsonArray.getJSONArray(0).getString(0));
            vipContact.setWeixin(jsonArray.getJSONArray(0).getString(1));
            vipContact.setQq(jsonArray.getJSONArray(0).getString(2));
            vipContact.setEmail(jsonArray.getJSONArray(0).getString(3));
            vipContact.setAddress(jsonArray.getJSONArray(0).getString(4));
            return vipContact;
        }

//            vipContacts.add(vipContact);
//        }

        return null;
    }

    private MultiValueMap Data(String vipId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'MOBIL'");//电话
        colums.add("'WEIBO'");//微信
        colums.add("'MSNQQ'");//QQ
        colums.add("EMAIL");//邮箱
        colums.add("ADDRESS");//地址

        params.put("columns", colums);

        params.put("table", "'C_V_VIP'");//表名
        params.put("count","true");

        HashMap paramss = new HashMap();
        paramss.put("column", "ID");
        paramss.put("condition", "'="+vipId+"'");
        params.put("params",paramss);
        System.err.println();

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getVipContactInfo g = new getVipContactInfo("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {//3125
            System.err.println(g.getData("185883"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
